﻿using System;
using System.Collections.Generic;
using System.Linq;
using WebReferenceManager.ServiceReference1;
using WindowSystem;

namespace WindowsGame
{
    public sealed class ContinueGame : Window
    {
        public ComboBox GameName, Faction;
        private readonly WebReferenceManager.ServerConnection serverConnection;
        private Game1 game;
        private GUIManager gui;
        private IEnumerable<GameDto> games;
        private Label Turn, TurnEnds;
        public GameDto GameDto;

        public ContinueGame(Game1 game, GUIManager gui, WebReferenceManager.ServerConnection serverConnection, int gameId) : this(game, gui, serverConnection)
        {
            GameName.SelectedItemKey = gameId;
        }

        public ContinueGame(Game1 game, GUIManager gui, WebReferenceManager.ServerConnection serverConnection) : base(game, gui)
        {
            this.game = game;
            this.gui = gui;
            this.serverConnection = serverConnection;

            Width = 330;
            Height = 250;
            const short controlPadding = 20;
            const short labelShift = 35;
            const short comboTextShift = 30;
            const short leftX = 15;
            const short leftX2 = leftX + 70;

            TitleText = "Continue Game";
            gui.Add(this);

            Label server;
            Add(new Label(game, gui) { Text = "Server:", X = leftX, Y = 15, Width = 50, Height = 20, });
            Add(server = new Label(game, gui) { Text = serverConnection.Address, X = leftX2, Y = 15 , Height = 20, });
            server.Width = Width - server.X - controlPadding;

            Add(new Label(game, gui) { Text = "Game", X = leftX, Y = 45, Width = 50, Height = 20, });
            GameName = new ComboBox(game, gui) { X = leftX2, Y = 40, };
            GameName.Width = Width - GameName.X - controlPadding;
            GameName.SelectionChanged += gameName_SelectionChanged;

            games = serverConnection.WebService.GetGames();

            foreach (var gameDto in games)
                GameName.AddEntry(gameDto.Id, gameDto.Name);
            Add(GameName);

            Add(new Label(game, gui) { Text = "Turn", X = leftX, Y = GameName.Y + labelShift, Width = 50, Height = 20, });
            Add(Turn = new Label(game, gui) { X = leftX2, Y = GameName.Y + labelShift, Height = 20, });
            Turn.Width = Width - Turn.X - controlPadding;

            Add(new Label(game, gui) { Text = "Turn Ends", X = leftX, Y = Turn.Y + labelShift, Width = 70, Height = 20, });
            Add(TurnEnds = new Label(game, gui) { X = leftX2, Y = Turn.Y + labelShift, Height = 20, });
            TurnEnds.Width = Width - TurnEnds.X - controlPadding;

            Add(new Label(game, gui) { Text = "Faction", X = leftX, Y = TurnEnds.Y + labelShift, Width = 50, Height = 20, });
            Faction = new ComboBox(game, gui) { X = leftX2, Y = TurnEnds.Y + comboTextShift, };
            Faction.Width = Width - Faction.X - controlPadding;
            Add(Faction);

            var btnPlay = new TextButton(game, gui) { Text = "Play", X = leftX, Y = Height - 65, };
            btnPlay.Click += (btnPlay_Click);
            Add(btnPlay);

            CenterWindow();
        }

        void gameName_SelectionChanged(UIComponent sender)
        {
            GameDto = games.Single(o => o.Id == GameName.SelectedItemKey);

            var factions = GameDto.FactionDtos;
            if (factions == null)
            {
                new MessageBox(game, gui, "No factions exist for this game. Please choose another game.", "No factions", MessageBoxButtons.OK, MessageBoxType.Error).Show(true);
                return;
            }

            Turn.Text = GameDto.Turn.ToString();
            TurnEnds.Text = String.Format("{0:u}", GameDto.TurnEnds);

            Faction.Clear();
            foreach (var faction in factions)
                Faction.AddEntry(faction.Id, faction.Name);
        }

        void btnPlay_Click(UIComponent sender)
        {
            if (string.IsNullOrEmpty(GameName.SelectedText))
            {
                (new MessageBox(game, gui, "Please select a game", "No game selected", MessageBoxButtons.OK, MessageBoxType.Error)).Show(true);
                return;
            }
            if (string.IsNullOrEmpty(Faction.SelectedText))
            {
                (new MessageBox(game, gui, "Please select a faction", "No faction selected", MessageBoxButtons.OK, MessageBoxType.Error)).Show(true);
                return;
            }
            CloseWindow();
        }

        
    }
}